//
//  OrderPaymentController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 16/6/21.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "OrderPaymentController.h"
#import "OrderPaymentCell.h"
#import "Order.h"
#import <AlipaySDK/AlipaySDK.h>
#import "APAuthV2Info.h"

@interface OrderPaymentController ()

{
    NSInteger PayType;
}

@property(nonatomic,strong)NSMutableArray *boolArray;

@end

@implementation OrderPaymentController

- (NSMutableArray *)boolArray
{
    if (!_boolArray) {
        _boolArray = [NSMutableArray arrayWithObjects:@"0",@"1",@"0",@"0", @"0", nil];
    }
    return _boolArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.scrollEnabled = false;
    self.title = @"订单支付";
    PayType = 1;
}

#pragma mark>>>>>>>>DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else{
        return 5;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier= @"OrderPaymentControllerCell";
    
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell==nil) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.font = FONT(15);
        cell.detailTextLabel.font = FONT(15);
    }
    
    if (indexPath.row == 0 && indexPath.section == 0) {
        cell.textLabel.text = @"订单金额";
        cell.detailTextLabel.text = @"¥200.00";
    }else if (indexPath.section ==1 && indexPath.row == 0){
        cell.textLabel.text = @"支付方式";
    }else{
        
        static NSString *OrderPaymentCellID= @"OrderPaymentConCell";
        
        OrderPaymentCell *cell=[tableView dequeueReusableCellWithIdentifier:OrderPaymentCellID];
       
        if (cell==nil) {
            cell= [[[NSBundle mainBundle] loadNibNamed:@"OrderPaymentCell" owner:nil options:nil] lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

        }

        if ([self.boolArray[indexPath.row] isEqualToString:@"1"]) {
            cell.selectImg.image = [UIImage imageNamed:@"shoujichongzhi_0"];
        } else {
            cell.selectImg.image = [UIImage imageNamed:@"shoujichongzhi_1"];
        }
        cell.selectIcon.image = [UIImage imageNamed:[NSString stringWithFormat:@"dingdanzhifu_%ld",indexPath.row]];
        
        NSArray *arr = @[@"积分",@"现金",@"微信",@"支付宝"];
        cell.payTittle.text = [NSString stringWithFormat:@"%@",arr[indexPath.row - 1]];
        return cell;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        return;
    }
    
    for (NSInteger i=0; i<self.boolArray.count; i++) {
        if (indexPath.row == i) {
            self.boolArray[i] = @"1";
        }else{
            self.boolArray[i] = @"0";
        }
    }
    PayType = indexPath.row;
    [self.tableView reloadData];
    NSLog(@"%ld",indexPath.row);
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 7;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section ==0) {
        return 0.01;
    }else{
        return HEIGHT/5;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return nil;
    }
    return [self payViewSetupUI];
}

- (UIView *)payViewSetupUI
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0,0, WIDTH, HEIGHT/5)];
    UIButton *rechargeBtn = [[UIButton alloc] init];
    rechargeBtn.tag = 999;
    [rechargeBtn setTitle:@"立即充值" forState:UIControlStateNormal];
    [view addSubview:rechargeBtn];
    rechargeBtn.layer.cornerRadius = 5;
    rechargeBtn.layer.masksToBounds = YES;
    rechargeBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [rechargeBtn setBackgroundColor:DEFAULT_COLOR];
    
    [rechargeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.width.mas_equalTo(WIDTH-30);
        make.centerY.equalTo(view.mas_centerY);
        make.left.equalTo(view.mas_left).offset(15);
        make.height.mas_equalTo(35);
    }];
    
    [rechargeBtn addTarget:self action:@selector(rechargeBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
    
    return view;
}

- (void)rechargeBtnDidClick{
    if(PayType == 1) {
        NSLog(@"积分")
    } else if (PayType == 2) {
        NSLog(@"现金");
    } else if (PayType == 3) {
        NSLog(@"微信");
    } else {
        NSLog(@"支付宝");
    }
}

@end
